//
//  TodayScrollView.m
//  EveryDayNews
//
//  Created by 徐继垚 on 14/11/28.
//  Copyright (c) 2014年 徐继垚. All rights reserved.
//

#import "TodayScrollView.h"

@implementation TodayScrollView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.dataSource = [NSMutableArray array];
      [self getdata];
     
    }
    return self;
}
-(void)getdata
{
    
    AFHTTPNetGetData * data = [[AFHTTPNetGetData alloc]init];
    [data getDataURL:@"http://news-at.zhihu.com/api/3/stories/latest?client=0" parameters:nil block:^(id result) {
        for (NSDictionary * dic in [result objectForKey:@"top_stories"]) {
            TodayHotModel * model = [[TodayHotModel alloc]init];
            model.tittle = [dic objectForKey:@"title"];
            model.myid = [dic objectForKey:@"id"];
            model.aimage = [dic objectForKey:@"image"];
            [self.dataSource addObject:model];
        }
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320 * Width, 200 * Height)];
        self.scrollView
        .contentSize = CGSizeMake(320 * 5 * Width, 200 *Height);
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.pagingEnabled = YES;
        [self addSubview:_scrollView];
        for (int i  = 0 ; i <4 ; i++) {
          _image = [[UIImageView alloc]initWithFrame:CGRectMake(320 * i  * Width, 0 * Height, 320 * Width, 200 * Height)];
            _image.contentMode = UIViewContentModeScaleAspectFill;
           [_image setImageWithURL:[NSURL URLWithString:[_dataSource[i] aimage] ] placeholderImage:[UIImage imageNamed:@"loading.jpeg"]];
             [self.scrollView addSubview:_image];
  
            
            UIImageView * imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320 * Width    , 100 * Height)];
            imageview.image = [UIImage imageNamed:@"layertop.png"];
            [self.image addSubview:imageview];
            UIImageView * imageview2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100 * Height,Width * 320,Height * 100)];
            imageview2.image = [UIImage imageNamed:@"layerdown.png"];
            [self.image addSubview:imageview2];
            
            _label = [[UILabel alloc]initWithFrame:CGRectMake(10 * Width, 115 * Height, 300 * Width, 80 * Height)];
            //  _label.backgroundColor = [UIColor redColor];
            
            _label.text = [_dataSource[i]tittle];
            _label.textColor = [UIColor whiteColor];
            _label.numberOfLines = 0;
            _label.font = [UIFont systemFontOfSize:20];
            [self.image addSubview:_label];
        }
        
        
        
        
        
        [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(runTimePage) userInfo:nil repeats:YES];
        
        _page = [[UIPageControl alloc] initWithFrame:CGRectMake(-10, 180 * Height ,Width * 320,Height * 10)];
        _page.numberOfPages = 5;
        //设置点的颜色
        _page.currentPageIndicatorTintColor = [UIColor whiteColor];
        [_page addTarget:self action:@selector(pageAction) forControlEvents:UIControlEventValueChanged];
        [self addSubview:_page];
        
        

        
    }];
    
}

- (void)pageAction
{
    [_scrollView setContentOffset:CGPointMake(Width * (_page.currentPage * 100), 0) animated:YES];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView //代理
{
    _page.currentPage = scrollView.contentOffset.x / 110;
}
- (void)turnPage
{
    NSInteger page = _page.currentPage; // 获取当前的page
  
    [_scrollView scrollRectToVisible:CGRectMake(Width * 320 * (page),0, Width * 320, Height * 110) animated:NO]; // 触摸pagecontroller那个点点 往后翻一页 +1
    [_page addTarget:self action:@selector(runTimePage) forControlEvents:UIControlEventValueChanged];
}
// 定时器绑定的方法
- (void)runTimePage
{
    NSInteger page = _page.currentPage; // 获取当前的page
    page++;
    page = page > 5 - 1 ? 0 : page ;
    _page.currentPage = page;
    
   
     // [_scrollView setContentOffset:CGPointMake(_page.currentPage * 320, 0) animated:YES];
   [self turnPage];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
